import type { Result } from "../util/handler";
import { handleResponse } from "../util/handler";

async function createStore({ storeName, token }: { storeName: string; token: string }): Promise<Result<null>> {
  return await fetch('/api/store/create', {
    method: 'post',
    credentials: 'same-origin',
    body: new URLSearchParams({
      storeName
    }),
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      'token': token
    }
  }).then((resp) => handleResponse<null>(resp));
}

export interface StoreInfo {
  storeId: number;
  storeName: string;
  userId: number;
}

async function findAllStore(token: string): Promise<Result<StoreInfo[]>> {
  return await fetch('/api/store/findAllStore', {
    method: 'post',
    credentials: 'same-origin',
    headers: {
      'content-type': 'application/json',
      'token': token
    }
  }).then((resp) => handleResponse<StoreInfo[]>(resp));
}

export {
  createStore,
  findAllStore
}